package com.bai.serviceOA.Controller;


import com.bai.common.Result;
import com.bai.common.Utils.MD5;
import com.bai.model.system.SysUser;
import com.bai.model.vo.SysUserQueryVo;
import com.bai.serviceOA.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-05-06
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService service;

    //TODO:用户条件分页查询
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo
    ) {
        //创建一个page对象
        Page<SysUser> pageParam = new Page<>(page, limit);
        //，封装对象
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        //从vo里获取条件值
        String keyword = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
        //判断条件值不为空
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like(SysUser::getUsername, keyword);
        }
        if (!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le(SysUser::getCreateTime, createTimeEnd);
        }
        //调用mapper的方法实现条件分页查询
        IPage<SysUser> pageModel = service.page(pageParam, wrapper);

        return Result.ok(pageModel);
    }

    //根据id查询用户
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        SysUser user = service.getById(id);
        return Result.ok(user);
    }

    //添加用户
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public Result save(@RequestHeader SysUser user){
        //密码进行加密存储 MD5
        String password = user.getPassword();
        String PasswordMD5 = MD5.encrypt(password);
        user.setPassword(PasswordMD5);
        service.save(user);
        return Result.ok();
    }

    //修改用户
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation(value = "更新用户")
    @PutMapping("update")
    public Result updateById(@RequestBody SysUser user){
        service.updateById(user);
        return Result.ok();
    }

    //删除用户
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        service.removeById(id);
        return  Result.ok();
    }

    //更改用户状态
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    @ApiOperation("根据用户id更改状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status
                               ){
        service.updateStatus(id, status);
         return  Result.ok();
    }


}

